# Copyright (c) 2008-2009 Mikhail Chernov 
#
# This file is part of dcExplorer.
#
# dcExplorer is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# dcExplorer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with dcExplorer.  If not, see <http://www.gnu.org/licenses/>.

package dcExplorer::layer::Grid;
use strict;
use Tools;

sub new
{
	my $proto = shift;
	my $class = ref($proto) || $proto;
	my $self = {};
	$self->{SmallStep} = shift;
	$self->{LargeStep} = shift;
	bless ($self, $class);
	return $self;
}

sub __simple
{
	my $a = shift;
	return ($a - 1) % 2000 + 1;
}

sub showGrid
{
	my $self = shift;
	my $canvas = shift;
	my $step = shift;
	my $color = shift;
	for (my $x = $canvas->{min_x} + (($step - $canvas->{min_x}) % $step); $x <= $canvas->{max_x}; $x += $step)
	{
		$canvas->Line($canvas->centerX($x), $canvas->{bord_y} + 1, $canvas->centerX($x), $canvas->{size_y} - $canvas->{bord_y} - 1, $color);
	}
	for (my $y = $canvas->{min_y} + (($step - $canvas->{min_y}) % $step); $y <= $canvas->{max_y}; $y += $step)
	{
		$canvas->Line($canvas->{bord_x} + 1, $canvas->centerY($y), $canvas->{size_x} - $canvas->{bord_x} - 1, $canvas->centerY($y), $color);
	}
}

sub showCoords
{
	my $self = shift;
	my $canvas = shift;
	my $step = shift;
	my $color = shift;
	for (my $x = $canvas->{min_x} + (($step - $canvas->{min_x}) % $step); $x <= $canvas->{max_x}; $x += $step)
	{
		$canvas->Text($canvas->centerX($x), $canvas->{bord_y},					 0,  1, 
			Tools::formatCoordinate(__simple($x)), $color);
		$canvas->Text($canvas->centerX($x), $canvas->{size_y} - $canvas->{bord_y}, 0, -1, 
			Tools::formatCoordinate(__simple($x)), $color);
	}
	for (my $y = $canvas->{min_y} + (($step - $canvas->{min_y}) % $step); $y <= $canvas->{max_y}; $y += $step)
	{
		$canvas->Text($canvas->{bord_x},					  $canvas->centerY($y),  1, 0, 
			Tools::formatCoordinate(__simple($y)), $color);
		$canvas->Text($canvas->{size_x} - $canvas->{bord_x},  $canvas->centerY($y), -1, 0, 
			Tools::formatCoordinate(__simple($y)), $color);
	}	
}

sub display
{
	my $self = shift;
	my $canvas = shift;
	$self->showGrid($canvas, $self->{SmallStep}, dcExplorer::canvas::Color->new(127, 127, 127));
	$self->showGrid($canvas, $self->{LargeStep}, dcExplorer::canvas::Color->new(127, 191, 127));
	$self->showCoords($canvas, $self->{SmallStep} * 2, dcExplorer::canvas::Color->new(127, 127, 127));
}

1;

